package com.hl.card.modular.pack.controller;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.baomidou.mybatisplus.plugins.Page;
import com.hl.card.common.controller.BaseController;
import com.hl.card.common.persistence.model.Recharge;
import com.hl.card.common.persistence.model.ckeck.CurrencyCheck;
import com.hl.card.common.persistence.model.ckeck.ExchangeCheck;
import com.hl.card.common.persistence.model.ckeck.RechargeCheck;
import com.hl.card.common.persistence.model.ckeck.RechargeUserCheck;
import com.hl.card.common.util.ResponseObject;
import com.hl.card.modular.pack.service.RechargeService;

@Controller
@RequestMapping("/api")
public class RechargeListController extends BaseController {
    @Autowired
    private RechargeService rechargeService;
    
    private final String RECHARGE="RECHARGE";//充值
    private final String WITHDRAW="WITHDRAW";//提币
    private final String EXCHANGE="EXCHANGE";//兑换

    /**
     * 充值记录添加
     */
    @RequestMapping(value = "/recharge/add")
    @ResponseBody
    public Object rechargeAdd(@Validated(value=RechargeCheck.class)Recharge recharge){
    	ResponseObject responseObject = new ResponseObject();
    	recharge.setExtra(RECHARGE);
    	
    	if (recharge.insert()) {
    		responseObject.setErroCode(1);
    		responseObject.setData("操作成功");
		}else {
			responseObject.setErroCode(0);
    		responseObject.setData("操作失败");
		}
    	return responseObject;
    }
    /**
     * 提现记录添加
     */
    @RequestMapping(value = "/currency/add")
    @ResponseBody
    public Object currencyAdd(@Validated(value=CurrencyCheck.class)  Recharge recharge){
    	ResponseObject responseObject = new ResponseObject();
    	recharge.setExtra(WITHDRAW);
    	if (recharge.insert()) {
    		responseObject.setErroCode(1);
    		responseObject.setData("操作成功");
		}else {
			responseObject.setErroCode(0);
    		responseObject.setData("操作失败");
		}
    	return responseObject;
    }
    /**
     * 兑换记录添加
     */
    @RequestMapping(value = "/exchange/add")
    @ResponseBody
    public Object exchangeAdd(@Validated(value=ExchangeCheck.class)  Recharge recharge){
    	ResponseObject responseObject = new ResponseObject();
    	recharge.setExtra(EXCHANGE);
    	if (recharge.insert()) {
    		responseObject.setErroCode(1);
    		responseObject.setData("操作成功");
		}else {
			responseObject.setErroCode(0);
    		responseObject.setData("操作失败");
		}
    	return responseObject;
    }

    /**
     * 修改
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    public Object update(@Validated(value=RechargeUserCheck.class)Recharge recharge){
    	ResponseObject responseObject = new ResponseObject();
    	
    	if (rechargeService.auditing(recharge)>0) {
    		responseObject.setErroCode(1);
    		responseObject.setData("操作成功");
		}else {
			responseObject.setErroCode(0);
    		responseObject.setData("操作失败");
		}
    	return responseObject;
    }

    /**
     * 获取列表
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list(@Validated(value=RechargeUserCheck.class)Recharge recharge) {
//    	recharge.setExtra(EXTRA);
    	Page<Recharge> dPage = rechargeService.selectMultiCondition(recharge);
    	 Map<String, Object> jsonMap = new HashMap<String, Object>();
		jsonMap.put("total", dPage.getTotal());
		jsonMap.put("rows", dPage.getRecords());
		jsonMap.put("current", dPage.getCurrent());
		jsonMap.put("pages", dPage.getPages());
        
        return jsonMap;
    }
    /**
     * 合同详情
     */
    @RequestMapping(value = "/rechargeDetail")
    public Object rechargeDetail(String recharge_no, Model model){
    	Recharge recharge= new Recharge(recharge_no);
    	recharge.setExtra(REDIRECT);
    	Map<String, Object> map = rechargeService.select(recharge);
        return map;
    }
	@RequestMapping(value = "/getEthPrice")
	@ResponseBody
    public String getEthPrice(){
	return	rechargeService.getEthReal();
	}
	@RequestMapping(value = "/getGusdPrice")

	@ResponseBody
	public String getGusdPrice(){
		String rateStr=rechargeService.getGUSDReal();
		BigDecimal bg=new BigDecimal(rateStr).setScale(2,BigDecimal.ROUND_DOWN);
    	return	bg.toString();
	}

}
